<?php
namespace TwinPeaks\Core\Datatype;

use TwinPeaks\Core\CCommonException;

abstract class CBase
{
	/**
	 * @var mixed
	 */
	protected $value;
	
	/**
	 * @var string
	 */
	protected $type;
	
	/**
	 * @var boolean
	 */
	protected $sanitizeFlag = false;
		
	/**
	 * @return boolean
	 */
	abstract function isValid();
	
	/**
	 * @return mixed
	 */
	abstract protected function getSanitizedValue();
	
	/**
	 * @return void
	 */
	function __construct($value = null)
	{
		$this->value = $value;
	}
	
	/**
	 * @return mixed
	 */
	function get()
	{
		if (!$this->isValid())
		{
			if ($this->sanitizeFlag)
				return $this->getSanitizedValue();
			else
				CCommonException::invalidValue($this->type, $this->value)->_throw();
		}

		return $this->value;
	}
	
	/**
	 * @return void
	 */
	function set($value)
	{
		$this->value = $value;
	}
	
	/**
	 * @return
	 */
	function __toString()
	{
		return $this->get();
	}
}
